<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" v-auth="'shop:xtlog:u8XtLog:view'" @click="handleExport()">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <a-popconfirm
          title="是否确认重新协同？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handleAgain()"
        >
          <a-button type="primary" v-auth="'shop:xtlog:u8XtLog:edit'">
            <Icon icon="simple-line-icons:action-undo" /> {{ t('重新协同') }}
          </a-button>
        </a-popconfirm>
        <a-popconfirm
          title="是否确认关闭协同？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handleClose()"
        >
          <a-button type="error" v-auth="'shop:xtlog:u8XtLog:edit'">
            <Icon icon="simple-line-icons:close" /> {{ t('关闭协同') }}
          </a-button>
        </a-popconfirm>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.vouchId }}
        </a>
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsShopXtlogU8XtLogList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { u8XtLogListData, reStartXt, closeXt } from '/@/api/shop/xtlog/u8XtLog';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';

  const { t } = useI18n('shop.xtlog.u8XtLog');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('U8协同日志管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('来源单据'),
        field: 'vouchId',
        component: 'Input',
      },
      {
        label: t('顺序'),
        field: 'sortNum',
        component: 'Input',
      },
      {
        label: t('目标单据'),
        field: 'toDjtype',
        component: 'Input',
      },
      {
        label: t('执行状态'),
        field: 'xtStatus',
        component: 'Select',
        componentProps: {
          dictType: 'btdm_xt_status',
          allowClear: true,
        },
      },
      {
        label: t('账套'),
        field: 'acctid',
        component: 'Input',
      },
      {
        label: t('协同状态'),
        field: 'routeStatus',
        component: 'Select',
        componentProps: {
          dictType: 'btdm_xt_route_status',
          allowClear: true,
        },
        defaultValue: '1',
      },
      {
        label: t('U8单据号'),
        field: 'xtDjno',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('来源单据'),
      dataIndex: 'vouchId',
      key: 'a.vouch_id',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('顺序'),
      dataIndex: 'sortNum',
      key: 'a.sort_num',
      sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('目标单据'),
      dataIndex: 'toDjtype',
      key: 'a.to_djtype',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('执行状态'),
      dataIndex: 'xtStatus',
      key: 'a.xt_status',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'btdm_xt_status',
    },
    {
      title: t('执行结果'),
      dataIndex: 'cresult',
      key: 'a.cresult',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('账套'),
      dataIndex: 'acctid',
      key: 'a.acctid',
      sorter: true,
      width: 60,
      align: 'left',
    },
    {
      title: t('开始'),
      dataIndex: 'bsource',
      key: 'a.bsource',
      sorter: true,
      width: 60,
      align: 'left',
    },
    {
      title: t('结束'),
      dataIndex: 'bend',
      key: 'a.bend',
      sorter: true,
      width: 60,
      align: 'left',
    },
    {
      title: t('单据日期'),
      dataIndex: 'ddate',
      key: 'a.ddate',
      sorter: true,
      width: 100,
      align: 'center',
    },
    {
      title: t('最后执行时间'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('协同路线'),
      dataIndex: 'croute',
      key: 'a.croute',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('协同状态'),
      dataIndex: 'routeStatus',
      key: 'a.route_status',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'btdm_xt_route_status',
    },
    {
      title: t('U8单据'),
      dataIndex: 'xtDjno',
      key: 'a.xt_djno',
      sorter: true,
      width: 120,
      align: 'left',
    },
    {
      title: t('单据数'),
      dataIndex: 'djNum',
      key: 'a.dj_num',
      sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('重发数'),
      dataIndex: 'reTimes',
      key: 'a.re_times',
      sorter: true,
      width: 80,
      align: 'center',
    },
  ];

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: u8XtLogListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/shop/xtlog/u8XtLog/exportData',
      target: '_self',
    });
  }

  // 重新协同
  async function handleAgain() {
    if (getSelectRows().length && getSelectRows().length == 1) {
      const res = await reStartXt({ id: getSelectRows()[0].id });
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请选择一行数据！！');
    }
  }

  // 关闭协同
  async function handleClose() {
    if (getSelectRows().length && getSelectRows().length == 1) {
      const res = await closeXt({ id: getSelectRows()[0].id });
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请选择一行数据！！');
    }
  }

  function handleSuccess() {
    reload();
  }
</script>
